package com.si.anddos;

import com.si.anddos.dpadapters.AnddosDbAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ResourceCursorAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class HistorialListActivity extends OptionsMenuActivity {
	
	private AnddosDbAdapter mDbHelper;
	private ListView historialList;

	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.historial_list);
        setTitle(R.string.titleHistorialList);
        
        
        mDbHelper = new AnddosDbAdapter(this);
        
        // onClickListener for list of accounts - starts New account activity
        // with Intent object with rowID of selected account
        historialList = (ListView) findViewById(R.id.historialList);
        historialList.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            	 
            	 mDbHelper.open();
            	 Cursor HistorialCursor = mDbHelper.getAllHistorialElements();
            	 HistorialCursor.moveToPosition(position);
            	 
            	 //Obtain id of the search
            	 int identifier=HistorialCursor.getInt(HistorialCursor.getColumnIndex(AnddosDbAdapter.colHistorialID));
                 
            	 mDbHelper.close();
            	 
            	 if(identifier>0){
            		 Intent i = new Intent(HistorialListActivity.this, SearchActivity.class);
            		 i.putExtra("identifier", identifier);
            		 startActivity(i);
            	 }
            	 
            }
          });
        
        // filling the list from database
        

       // fillData();

        // register context menu for list of accounts
        registerForContextMenu(historialList);
    }
	
	
	 private void fillData() {
		 
	        // Get all of the rows from the database and create the item list
	    	mDbHelper.open();
	        Cursor HistorialCursor = mDbHelper.getAllHistorialElements();
	        startManagingCursor(HistorialCursor);
	        
	        // Create an array to specify the fields we want to display in the list (only TITLE)
	        String[] from = new String[]{AnddosDbAdapter.colHistorialName, 
	        		AnddosDbAdapter.colHistorialExtension,AnddosDbAdapter.colHistorialNumberResults,
	        		AnddosDbAdapter.colHistorialNumberSearchs,AnddosDbAdapter.colHistorialProviderRapidshare
	        		,AnddosDbAdapter.colHistorialProviderMegaupload,AnddosDbAdapter.colHistorialProviderFilesonic,
	        		AnddosDbAdapter.colHistorialProviderFileserve,AnddosDbAdapter.colHistorialProviderMegashares,
	        		AnddosDbAdapter.colHistorialProviderHotfile};
	        // and an array of the fields we want to bind those fields to 
	        int[] to = new int[]{R.id.TextNameHistorialRow,R.id.TextExtensionHistorialRow,
	        		R.id.TextResultsHistorialRow,R.id.TextSearchsHistorialRow,
	        		R.id.TextProvidersHistorialRapidshare,R.id.TextProvidersHistorialMegaupload,
	        		R.id.TextProvidersHistorialFilesonic,R.id.TextProvidersHistorialFileserve,
	        		R.id.TextProvidersHistorialMegashares,R.id.TextProvidersHistorialHotfile};

	        // Now create a simple cursor adapter and set it to display
	        SimpleCursorAdapter HistorialElements = 
	            new SimpleCursorAdapter(this, R.layout.historial_list_row, HistorialCursor, from, to);
	        
	        // make message to user if no accounts is in database
	        if(HistorialElements.getCount()==0)
	        	Toast.makeText(this, R.string.textNoHistorialElements, Toast.LENGTH_SHORT).show();
	        historialList.setAdapter(HistorialElements);
	   
	        mDbHelper.close();
	    }
	 
	 @Override
		public void onWindowFocusChanged(boolean hasFocus){
		    if(hasFocus){
		    	fillData();
		    	
		    }
	 }
}